# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from time import sleep
from itemadapter import ItemAdapter
from scrapy.exporters import CsvItemExporter

class DoubanPipeline:
    def process_item(self, item, spider):
        return item


class CsvExporterPipeline(object): 
    def __init__(self):#每次修改init函数的前两行中写入的文件，和export的域就可以。修改完之后别忘了在setting中启动pipeline
        self.file = open("movie.csv","wb") #以二进制形式写入
        self.exporter = CsvItemExporter(self.file,fields_to_export=['serial_number','movie_name','movie_introduce','star','evaluate_number','describe'])
        self.exporter.start_exporting() #启动exporter，开始把item持久化到csv文件中去

    def process_item(self,item,spider):
        self.exporter.export_item(item)#开始传输
        return item

    def spider_closed(self,spider):
        self.exporter.finish_exporting() #结束传输
        self.file.close()

